package com.woniuxy.mini.controller;

import okhttp3.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/PostAi")
@CrossOrigin("*")
public class PostRequestController {

    @PostMapping("/Ai")
    public String postRequest(@RequestBody Map<String, String> requestBody) {
        System.out.println("已收到请求" + requestBody.get("ques"));
        String url = requestBody.get("url");
        String ques = requestBody.get("ques");
        String voiceType = requestBody.get("voiceType"); // 获取前端传来的voiceType值

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .build();

        FormBody.Builder formBuilder = new FormBody.Builder();
        formBuilder.add("ques", ques);
        formBuilder.add("isLongChat", "1");
        formBuilder.add("appKey", "667254987a9e4e5d3b6347f5");
        formBuilder.add("uid", "6YFmr61718768769906MHBDBuzA9x");
        formBuilder.add("voiceType", voiceType); // 添加voiceType参数

        FormBody bodyRequest = formBuilder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(bodyRequest)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            } else {
                return response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to send request";
        }
    }

    @PostMapping("/AiImage")
    public String postRequestImage(@RequestBody Map<String, String> requestBody) {
        String url = requestBody.get("url");
        String imgTxt = requestBody.get("imgTxt");
        String style = requestBody.get("style");
        String ratio = requestBody.get("ratio");

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .build();

        FormBody.Builder formBuilder = new FormBody.Builder();
        formBuilder.add("imgTxt", imgTxt);
        formBuilder.add("style", style);
        formBuilder.add("ratio", ratio);
        formBuilder.add("appKey", "667254987a9e4e5d3b6347f5");
        formBuilder.add("uid", "6YFmr61718768769906MHBDBuzA9x");

        FormBody bodyRequest = formBuilder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(bodyRequest)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            } else {
                return response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to send request";
        }
    }






}
